/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIUSer.java
 *
 * Created on 16-10-2011, 22:44:09
 */
package gui;

import Config.Localization;
import bl.BLBranch;
import bl.BLUser;
import bl.BLUserType;
import entities.Branch;
import entities.User;
import entities.UserType;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HungVM
 */
public final class GUIUser extends javax.swing.JPanel {

    ArrayList<User> users = null;
    ArrayList<UserType> userTypes = null;
    ArrayList<Branch> branchs = null;
    BLUser blUser;
    User user;
    DefaultTableModel mode;

    /** Creates new form GUIUSer */
    public GUIUser() {
        initComponents();
        users = new ArrayList<User>();
        blUser = new BLUser();
        loadUser();
        loadData();
        loadBranch();
        loadUserType();
        //loadComBo();
        this.btnEdit.setEnabled(false);
        this.btnDelete.setEnabled(false);
        this.btnAdd.setEnabled(false);
        this.btnCheck.setEnabled(false);
        this.btnLock.setEnabled(false);
        canEdit(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btgGender = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lblUserId = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        txtFullName = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtBirthDay = new com.toedter.calendar.JDateChooser();
        txtIdentity = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        rdoMale = new javax.swing.JRadioButton();
        rdoFemale = new javax.swing.JRadioButton();
        cboUserType = new javax.swing.JComboBox();
        cboBranch = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        taNote = new javax.swing.JTextArea();
        btnLock = new javax.swing.JButton();
        btnCheck = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        txtSearch = new javax.swing.JTextField();
        cboSearch = new javax.swing.JComboBox();
        btnSearch = new javax.swing.JButton();
        btnList = new javax.swing.JButton();
        btnCreateNew = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblUser = new javax.swing.JTable();

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("UserID");

        jLabel2.setText("UserName:");

        jLabel3.setText("Full Name:");

        jLabel4.setText("Address:");

        jLabel5.setText("BirthDay:");

        jLabel6.setText("Email:");

        jLabel7.setText("Identity:");

        jLabel8.setText("Gender:");

        jLabel9.setText("Branch:");

        jLabel10.setText("User Type");

        jLabel11.setText("Phone:");

        lblUserId.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblUserId.setText("Id");

        txtBirthDay.setDateFormatString("MM/DD/YYYY");

        jLabel13.setText("Note :");

        btgGender.add(rdoMale);
        rdoMale.setText("Male");

        btgGender.add(rdoFemale);
        rdoFemale.setText("Female");

        cboUserType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cboBranch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        taNote.setColumns(20);
        taNote.setRows(5);
        jScrollPane1.setViewportView(taNote);

        btnLock.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/_lock.png"))); // NOI18N
        btnLock.setText("Lock");
        btnLock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLockActionPerformed(evt);
            }
        });

        btnCheck.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/check (2).png"))); // NOI18N
        btnCheck.setText("Check");
        btnCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCheckActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6))
                .addGap(33, 33, 33)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                    .addComponent(txtIdentity, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(lblUserId, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtFullName, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtAddress, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))
                    .addComponent(txtBirthDay, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel8)
                                .addComponent(jLabel11)
                                .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jLabel9)
                            .addComponent(jLabel13)))
                    .addComponent(btnLock))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(rdoMale)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rdoFemale))
                    .addComponent(txtPhone, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                    .addComponent(cboUserType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cboBranch, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addGap(71, 71, 71))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblUserId)
                            .addComponent(jLabel1))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnCheck))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(rdoMale)
                            .addComponent(rdoFemale))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(cboUserType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(txtBirthDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(txtIdentity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel7))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btnLock)
                                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel6)))
                            .addComponent(jLabel13)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(cboBranch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        txtSearch.setColumns(16);
        txtSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSearchActionPerformed(evt);
            }
        });
        jPanel3.add(txtSearch);

        cboSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "UserID", "UserName", "FullName", "Identity", "Locked" }));
        jPanel3.add(cboSearch);

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/find.png"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        jPanel3.add(btnSearch);

        btnList.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/list1.png"))); // NOI18N
        btnList.setText("View all");
        btnList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListActionPerformed(evt);
            }
        });
        jPanel3.add(btnList);

        btnCreateNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/new2.png"))); // NOI18N
        btnCreateNew.setText("Create New");
        btnCreateNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateNewActionPerformed(evt);
            }
        });
        jPanel3.add(btnCreateNew);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/add_small.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel3.add(btnAdd);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Text-Edit-icon.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        jPanel3.add(btnEdit);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/DeleteRed.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jPanel3.add(btnDelete);

        tblUser.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblUser.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblUserMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tblUser);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 790, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29))
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:
    User u = initUser();
    
    
    if(u!=null){
        if(blUser.checkIdentity(txtIdentity.getText().trim())){
            JOptionPane.showMessageDialog(null, Localization.getMessage("Msg14"));
            return;
        }
        if(blUser.insertUser(u)){
            JOptionPane.showMessageDialog(null, Localization.getMessage("Success"));
            this.loadUser();
        }else{
            if(BLUser.f == 1){
                JOptionPane.showMessageDialog(null, Localization.getMessage("Msg8"));
            }
            JOptionPane.showMessageDialog(null, Localization.getMessage("Fail"));
        }
    }
        
            
    
}//GEN-LAST:event_btnAddActionPerformed

private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
// TODO add your handling code here:
    User u = initUser();   
    u.setUserId(Integer.parseInt(lblUserId.getText()));
    if(u!=null){
        if(blUser.updateUser(u)){
            JOptionPane.showMessageDialog(null, Localization.getMessage("Success"), "Infomation", JOptionPane.INFORMATION_MESSAGE);
            this.loadUser();
        }  
        else{
            JOptionPane.showMessageDialog(null, Localization.getMessage("Fail"), "Infomation", JOptionPane.ERROR_MESSAGE);
        }
            
    }
}//GEN-LAST:event_btnEditActionPerformed

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
// TODO add your handling code here:
    try {
        int n = Integer.parseInt(lblUserId.getText());
        if(n == user.getUserId()){
            JOptionPane.showMessageDialog(null, Localization.getMessage("Msg15"), "Infomation", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        int choice = JOptionPane.showConfirmDialog(null, Localization.getMessage("Msg11"), "Confirm", JOptionPane.OK_CANCEL_OPTION);
                if(choice == 0){
                    if(blUser.deleteUser(n)){
                        JOptionPane.showMessageDialog(null, Localization.getMessage("Success"), "Information", JOptionPane.INFORMATION_MESSAGE);
                        loadUser();
                    }else{
                        JOptionPane.showMessageDialog(null, Localization.getMessage("Fail"), "Information", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, Localization.getMessage("Fail"), "Information1", JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_btnDeleteActionPerformed

private void btnLockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLockActionPerformed
// TODO add your handling code here:
        try {
            if(btnLock.getText().equals("Lock")){
                if(blUser.lockUser(Integer.parseInt(lblUserId.getText()))){
                    JOptionPane.showMessageDialog(this, Localization.getMessage("Msg9"));
                    this.btnLock.setText("UnLock");
                    this.loadUser();
                }
            }
            else{
                if(blUser.unLockUser(Integer.parseInt(lblUserId.getText()))){
                    JOptionPane.showMessageDialog(this, Localization.getMessage("Msg10"));
                    this.btnLock.setText("Lock");
                    this.loadUser();
                }
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error");
        }
}//GEN-LAST:event_btnLockActionPerformed

private User initUser(){
    User u = new User();
     if(!blUser.validateUserName(txtUserName.getText()))
        return null;
    if(!validateUser())
        return null;
    if(!blUser.validateEmail(txtEmail.getText()))
        return null;
    if(!blUser.validateIdentity(txtIdentity.getText()))
        return null;
    u.setAddress(txtAddress.getText());
    if(txtBirthDay.getDate() !=null){
        java.sql.Date date = new java.sql.Date(txtBirthDay.getDate().getTime());
        u.setBirthDay(date);
    }
    Branch bra = new Branch();
    bra = (Branch)cboBranch.getSelectedItem();
    
    u.setBranchId(bra);
    u.setEmail(txtEmail.getText());
    u.setFullName(txtFullName.getText());
    u.setIdentity(txtIdentity.getText().trim());
    u.setNote(taNote.getText());
    u.setPhone(txtPhone.getText());
    u.setSex(rdoMale.isSelected());
    u.setUserName(txtUserName.getText());
    
    UserType uType = new UserType();
    uType = (UserType)cboUserType.getSelectedItem();
    u.setUserType(uType);
    return u;
}

private void btnCreateNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateNewActionPerformed
// TODO add your handling code here:
    createNew();
    btnAdd.setEnabled(true);
    btnDelete.setEnabled(false);
    btnEdit.setEnabled(false);
    btnCheck.setEnabled(true);
    btnLock.setEnabled(false);
    lblUserId.setText("");
    canEdit(true);
}//GEN-LAST:event_btnCreateNewActionPerformed

private void btnCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCheckActionPerformed
// TODO add your handling code here:

    if(blUser.validateUserName(txtUserName.getText())){
        if(blUser.checkUser(txtUserName.getText().trim())){
            JOptionPane.showMessageDialog(null, Localization.getMessage("Msg7"), "Notice", JOptionPane.WARNING_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(null, Localization.getMessage("Msg8"), "Notice", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
}//GEN-LAST:event_btnCheckActionPerformed

private void txtSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSearchActionPerformed
// TODO add your handling code here:
    
}//GEN-LAST:event_txtSearchActionPerformed

private void btnListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListActionPerformed
// TODO add your handling code here:
    loadUser();
}//GEN-LAST:event_btnListActionPerformed

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
// TODO add your handling code here:
    int index = cboSearch.getSelectedIndex();
    switch(index){
        case 0: //search by Id
            try {
                int id = Integer.parseInt(txtSearch.getText());
                users = blUser.getById(id);
                if(users.size() ==0)
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
                loadData();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Msg13"));
            }
            break;
        case 1: //search by username
            
            users = blUser.getByUserName(txtSearch.getText());
            if(users.size() ==0)
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
                    loadData();
            break;
        case 2://search by fullName
            users = blUser.getByFullName(txtSearch.getText());
            if(users.isEmpty())
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
                    loadData();
            break;
        case 3://searchBy Identity
            users = blUser.getByIdentity(txtSearch.getText());
            if(users.isEmpty())
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
                    loadData();
            break;
        case 4: //serach user Lock
             users = blUser.getbyLock(1);
            if(users.isEmpty())
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
                    loadData();
            break;
            
    }
}//GEN-LAST:event_btnSearchActionPerformed

private void tblUserMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblUserMouseReleased
// TODO add your handling code here:
    int row = this.tblUser.getSelectedRow();
    if(row >= users.size())
        return;
    lblUserId.setText(String.valueOf(users.get(row).getUserId()));
    user = users.get(row);
    txtFullName.setText(users.get(row).getFullName());
    txtUserName.setText(users.get(row).getUserName());
    txtAddress.setText(users.get(row).getAddress());
    //txtNgaySinh.setText(String.valueOf(users.get(row).getNgaySinh()));

    txtBirthDay.setDate(users.get(row).getBirthDay());

    txtEmail.setText(users.get(row).getEmail());
    txtIdentity.setText(users.get(row).getIdentity());
    txtPhone .setText(users.get(row).getPhone());
    taNote.setText(users.get(row).getNote());

    if (users.get(row).isIsLock()) {
        btnLock.setText("UnLock");
    } else {
        btnLock.setText("Lock");
    }
    
    cboBranch.getModel().setSelectedItem(users.get(row).getBranchId());
    cboUserType.getModel().setSelectedItem(users.get(row).getUserType());


    rdoMale.setSelected(user.isSex());
    rdoFemale.setSelected(!user.isSex());

    this.btnEdit.setEnabled(true);
    this.btnDelete.setEnabled(true);
    this.btnAdd.setEnabled(false);
    this.btnCheck.setEnabled(false);
    this.btnLock.setEnabled(true);
    canEdit(true);
}//GEN-LAST:event_tblUserMouseReleased


    private void createNew(){
        txtUserName.setText("");
        txtAddress.setText("");
        txtEmail.setText("");
        txtFullName.setText("");
        txtIdentity.setText("");
        txtPhone.setText("");
        txtBirthDay.setDate(null);
        taNote.setText("");
    }
    
    public void loadBranch() {
        try {
            BLBranch blBranch = new BLBranch();
            branchs = blBranch.getAllActive();
            DefaultComboBoxModel modeC = new DefaultComboBoxModel();
            for (int i = 0; i < branchs.size(); i++) {
                modeC.addElement(branchs.get(i));
            }
            this.cboBranch.setModel(modeC);
        } catch (Exception ex) {
            Logger.getLogger(GUIUser.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void loadUserType() {
        BLUserType blUserType = new BLUserType();
        userTypes = blUserType.getAll(); //cho het tat ca cac phong vao 1 ArrayList
        DefaultComboBoxModel modeC = new DefaultComboBoxModel();
        for (int i = 0; i < userTypes.size(); i++) {
            modeC.addElement(userTypes.get(i));
        }
        this.cboUserType.setModel(modeC);
    }

    private void loadData() { // Ham load du lieu tu danh sach users
        mode = new DefaultTableModel();
        mode.addColumn("User ID");
        mode.addColumn("Full Name");
        mode.addColumn("UserName");
        mode.addColumn("Birth Day");
        mode.addColumn("Address");
        mode.addColumn("Phone");
        mode.addColumn("Position");
        mode.addColumn("Branch Name");
        mode.setRowCount(users.size());
        int i = 0;
        for (User u : users) {
            mode.setValueAt(u.getUserId(), i, 0);
            mode.setValueAt(u.getFullName(), i, 1);
            mode.setValueAt(u.getUserName(), i, 2);
            mode.setValueAt(u.getBirthDay(), i, 3);
            mode.setValueAt(u.getAddress(), i, 4);
            mode.setValueAt(u.getPhone(), i, 5);
            mode.setValueAt(u.getUserType().getTypeName(), i, 6);
            mode.setValueAt(u.getBranchId().getBranchName(), i, 7);
            i++;
        }
        this.tblUser.setModel(mode);
    }

    public void loadUser() { // load lại dữ liệu từ bảng nhân viên
        users = new ArrayList<User>();
        try {
            users = blUser.getAll();
        } catch (Exception e) {
        }
        loadData();
    }

    public boolean validateUser() {
        List<JTextField> txts = new ArrayList<JTextField>();
        txts.add(txtIdentity);
        txts.add(txtFullName);
        txts.add(txtUserName);
        txts.add(txtEmail);
        txts.add(txtPhone);
        boolean check = true;
        for (int i = 0; i < txts.size(); i++) {
            if (txts.get(i).getText().length() == 0) {
                check = false;
            }
        }
        if (!check) {
            JOptionPane.showMessageDialog(this, Localization.getMessage("Msg11"));
        }
        return check;
    }    
    
    public void canEdit(boolean isEdit){
        txtAddress.setEditable(isEdit);
        txtBirthDay.setEnabled(isEdit);
        txtEmail.setEditable(isEdit);
        txtUserName.setEditable(isEdit);
        txtIdentity.setEditable(isEdit);
        txtPhone.setEditable(isEdit);
        txtSearch.setEditable(isEdit);
        taNote.setEditable(isEdit);
        txtFullName.setEditable(isEdit);
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btgGender;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCheck;
    private javax.swing.JButton btnCreateNew;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnList;
    private javax.swing.JButton btnLock;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cboBranch;
    private javax.swing.JComboBox cboSearch;
    private javax.swing.JComboBox cboUserType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblUserId;
    private javax.swing.JRadioButton rdoFemale;
    private javax.swing.JRadioButton rdoMale;
    private javax.swing.JTextArea taNote;
    private javax.swing.JTable tblUser;
    private javax.swing.JTextField txtAddress;
    private com.toedter.calendar.JDateChooser txtBirthDay;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JTextField txtIdentity;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtSearch;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
